﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Sintel.SPReportingWizard
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            tbxWebUrl.Text = ConfigurationManager.AppSettings["SiteUrl"];
            tbxDestinationDatabaseName.Text = ConfigurationManager.AppSettings["DestinationDatabaseName"];
            tbxSharepointDBName.Text = ConfigurationManager.AppSettings["SharePointDatabaseName"];
            tbxSharepointDatabaseServerName.Text = ConfigurationManager.AppSettings["SharePointDatabaseServerName"];
            tbxAllWebsTableName.Text = ConfigurationManager.AppSettings["AllWebsTableName"];

            string sharePointVersion = ConfigurationManager.AppSettings["SharePointVersion"];
            comboBoxVersion.Text = sharePointVersion;
        

            textBoxListNames.Text = ConfigurationManager.AppSettings["ListNames"];
            textBoxOwnerCode.Text = ConfigurationManager.AppSettings["ViewPrefix"];
            chbCreateDatabase.Checked =  ConfigurationManager.AppSettings["CreateDB"] == "Yes";
            this.Text += " ver. " + Application.ProductVersion.ToString();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {            
            if (String.IsNullOrEmpty(tbxSharepointDatabaseServerName.Text)) {
                MessageBox.Show("Please provide the SharePoint Server Name");
                return;
            }

            if (String.IsNullOrEmpty(tbxSharepointDBName.Text))
            {
                MessageBox.Show("Please provide the SharePoint Database Name");
                return;
            }

            if (String.IsNullOrEmpty(tbxAllWebsTableName.Text))
            {
                MessageBox.Show("Please provide the SQL Table Name");
                return;
            }    
   
            string connStr = ConfigurationManager.ConnectionStrings["ContentDatabaseConnectionString"].ConnectionString;
            if (connStr.Length == 0){
                connStr = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI;", tbxSharepointDatabaseServerName.Text, tbxSharepointDBName.Text);
            }

            ScriptGenerator generator = new ScriptGenerator(tbxWebUrl.Text, tbxSharepointDBName.Text, tbxDestinationDatabaseName.Text, chbCreateDatabase.Checked, connStr, tbxAllWebsTableName.Text, comboBoxVersion.Text, textBoxListNames.Text, textBoxOwnerCode.Text, rtxScript);
            generator.generate();
            if (rtxScript.Text != null & rtxScript.Text != "")
            {

                try
                {
                    Clipboard.SetText(rtxScript.Text);
                    MessageBox.Show("The SQL script was generated successfully and copied to your clipboard.\nPlease run the script on the database server in which you wish to create the views.\nHappy Reporting!");
                }
                catch
                {
                    MessageBox.Show("The SQL script was generated successfully.\nPlease run the script on the database server in which you wish to create the views.\nHappy Reporting!");
                }
            }
            
        }

        private void label22_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.sintel.ie");
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://sintelspreportingwizard.codeplex.com/license");
        }




    }
}
